package com.fafica.filtro;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.fafica.entidades.Inscrito;

/**
 * Servlet Filter implementation class FiltroInscrito
 */
@WebFilter(urlPatterns={"/inscricao.jsp", "/inscricao_passo_2.jsp", "/inscricao_passo_3.jsp", "/inscricao_passo_4.jsp", "/resumo_inscricao.jsp"})
public class FiltroInscrito implements Filter {

    /**
     * Default constructor. 
     */
    public FiltroInscrito() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) arg0;
		HttpServletResponse response = (HttpServletResponse) arg1;
		HttpSession sessao = request.getSession();
		
		try{
			
			Inscrito inscrito = (Inscrito) sessao.getAttribute("inscrito");
			if(inscrito != null){
				chain.doFilter(arg0, arg1);
			}
			else{
				response.sendRedirect("login_submit.jsp");
			}			
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
